package com.tensquare.gathering.service;

import com.tensquare.gathering.dao.GatheringDao;
import com.tensquare.gathering.pojo.Gathering;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2021/3/2 17:02
 * @Description
 * @version: 1.0.0
 */
@Service
@Transactional
public class GatheringService {
    @Autowired
    private GatheringDao gatheringDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 分页查询
     * @param gathering
     * @param page
     * @param size
     * @return
     */
    public Page findSearch(Gathering gathering, int page, int size) {
        Pageable pageable = PageRequest.of(page-1, size);
        Specification<Gathering> specification = new Specification<Gathering>() {
            @Override
            public Predicate toPredicate(Root<Gathering> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList();
                if(gathering.getName()!=null && !"".equals(gathering.getName())) {
                    Predicate name = criteriaBuilder.like(root.get("name").as(String.class), "%" + gathering.getName() + "%");
                    list.add(name);
                }
                if(gathering.getCity()!=null && !"".equals(gathering.getCity())) {
                    Predicate city = criteriaBuilder.like(root.get("city").as(String.class), "%" + gathering.getCity() + "%");
                    list.add(city);
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
        return gatheringDao.findAll(specification, pageable);
    }

    /**
     * 条件查询
     * @param gathering
     * @return
     */
    public List<Gathering> findSearch(Gathering gathering) {
        return gatheringDao.findAll(new Specification<Gathering>() {
            @Override
            public Predicate toPredicate(Root<Gathering> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(gathering.getName()!=null && !"".equals(gathering.getName())) {
                    Predicate name = criteriaBuilder.like(root.get("name").as(String.class), "%" + gathering.getName() + "%");
                    list.add(name);
                }
                if(gathering.getCity()!=null && !"".equals(gathering.getCity())) {
                    Predicate city = criteriaBuilder.like(root.get("city").as(String.class), "%" + gathering.getCity() + "%");
                    list.add(city);
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        });
    }

    /**
     * 查询所有
     * @return
     */
    public List<Gathering> findAll() {
        return gatheringDao.findAll();
    }

    /**
     * 跟据id查询
     * @param id
     * @return
     */
    @Cacheable(value = "gathering",key = "#id")
    public Gathering findById(String id) {
        return gatheringDao.findById(id).get();
    }

    /**
     * 增加
     * @param gathering
     */
    public void add(Gathering gathering) {
        gathering.setId(idWorker.nextId()+"");
        gatheringDao.save(gathering);
    }

    /**
     * 修改
     * @param gathering
     */
    @CacheEvict(value="gathering", key = "#gathering.id")
    public void update(Gathering gathering) {
        gatheringDao.save(gathering);
    }

    /**
     * 删除
     * @param id
     */
    @CacheEvict(value = "gathering", key = "#id")
    public void deleteById(String id) {
        gatheringDao.deleteById(id);
    }

}
